package com.itheima.lucene;

import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;


import javax.swing.text.AbstractDocument;
import java.io.File;
import java.io.IOException;

public class ManyIlndexWriter {
    public static void main(String[] args) throws IOException {
        //创建索引写入核心对象
        Directory directory = FSDirectory.open(new File("F:\\workspace27\\lucene\\index"));
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory, config);
        //添加文档数据
        Document doc1 = new Document();
        doc1.add(new LongField("pid", 3, Field.Store.YES));
        doc1.add(new StringField("title", "网易娱乐勇士逆转猛龙", Field.Store.YES));
        doc1.add(new TextField("content", "勇士经过苦战战胜猛龙", Field.Store.YES));
        Document doc2 = new Document();
        doc2.add(new LongField("pid", 4, Field.Store.YES));
        doc2.add(new StringField("title", "网易娱乐勇士杜兰特受伤", Field.Store.YES));
        doc2.add(new TextField("content", "杜兰特在与猛龙大战中受伤", Field.Store.YES));
        Document doc3 = new Document();
        doc3.add(new LongField("pid", 5, Field.Store.YES));
        doc3.add(new StringField("title", "勇士夺冠", Field.Store.YES));
        doc3.add(new TextField("content", "勇士经过苦战终于得到总冠军", Field.Store.YES));

        indexWriter.addDocument(doc1);
        indexWriter.addDocument(doc2);
        indexWriter.addDocument(doc3);
        //提交文档
        indexWriter.commit();
        //释放资源
        indexWriter.close();
        //词条查询


    }

    @Test
    //词条查询
    public void termQueryTest() throws Exception {
        TermQuery query = new TermQuery(new Term("content", "勇士"));

        publicQuery(query);

    }

    //通配符查询 *表示 0 到任意个字符 ? 表示占用一个符号
    @Test
    public void wildCardQuery() throws Exception {
        WildcardQuery query = new WildcardQuery(new Term("content", "勇?"));
        publicQuery(query);
    }

    @Test
//模糊查询
    public void fuzzyQueryTest() throws Exception {
        FuzzyQuery query = new FuzzyQuery(new Term("content", "总冠局"));

        publicQuery(query);

    }

    //数值查询
    @Test
    public void numericRangeQueryTest() throws Exception {

        NumericRangeQuery query = NumericRangeQuery.newLongRange("pid", 2L, 5L, true, true);
        publicQuery(query);


    }

    //组合查询
    @Test
    public void booleanQueryTest() throws Exception {
        BooleanQuery query = new BooleanQuery();
        NumericRangeQuery query1 = NumericRangeQuery.newLongRange("pid", 2L, 5L, true, true);
        TermQuery query2 = new TermQuery(new Term("content", "受伤"));
        query.add(query1, BooleanClause.Occur.MUST);
        query.add(query2, BooleanClause.Occur.SHOULD);
        publicQuery(query);

    }

    @Test
    //lucene的索引修改
    public void updateIndexTest() throws Exception {


        //创建 索引写入对象
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());
        Directory directory = FSDirectory.open(new File("F:\\workspace27\\lucene\\index"));
        IndexWriter indexWriter = new IndexWriter(directory, config);
        //创建一个替换后的doc对象
        Document doc = new Document();
        doc.add(new LongField("Pid", 7L, Field.Store.YES));
        doc.add(new StringField("title", "勇士错是总冠军", Field.Store.YES));
        doc.add(new TextField("content", "杜兰特受伤,勇士错失总冠军", Field.Store.YES));
        //执行修改操作
        indexWriter.updateDocument(new Term("content", "受伤"), doc);
        //提交数据
        indexWriter.commit();
        //释放资源
        indexWriter.close();


    }

    @Test
    public void delIndexTest() throws Exception {
        //创建索引写入器对象
        Directory directory = FSDirectory.open(new File("F:\\\\workspace27\\\\lucene\\\\index\""));
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, new IKAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory, config);
        indexWriter.deleteDocuments(new Term("content", "总冠军"));
        indexWriter.commit();
        indexWriter.close();

    }


    //抽取一个公共的查询的方法
    public static void publicQuery(Query query) throws Exception {

        IndexReader indexReader = DirectoryReader.open(FSDirectory.open(new File("F:\\workspace27\\lucene\\index")));


        //创建查询核心类对象
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        //执行查询,获取查询结果
        TopDocs topDocs = indexSearcher.search(query, Integer.MAX_VALUE);
        //从结果中获取数据
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;//得分文档数据
        int totalHits = topDocs.totalHits;//整个索引库有多少条记录相匹配
        for (ScoreDoc scoreDoc : scoreDocs) {
            float score = scoreDoc.score;//文档得分,得分越高,匹配度越高
            int docId = scoreDoc.doc;//文档id
            Document doc = indexSearcher.doc(docId);//通过文档id获得在索引库中的原始数据
            String pid = doc.get("pid");
            String title = doc.get("title");
            String content = doc.get("content");
            System.out.println("文档的得分: " + score + "文档的唯一id为:" + docId + " ; 文档的id: " + pid + "; 文档的标题:" + title + ";文档的内容: " + content);
        }

    }


}









